/**
 * Created by Egan  on 2015/1/17.
 */
module catgame{
    export class GameContainer extends egret.DisplayObjectContainer{

        private stageW:number;
        private stageH:number;
        private map:catgame.Map;//地图
        private cat:catgame.Cat;//猫子
        private btnStart:egret.Bitmap;//开始按钮
        private moreBtn:egret.Bitmap;//更多按钮
        private scorePanel:catgame.ScorePanel;//结算面板

        private successPanel:catgame.SuccessPanel;//赢面板
        private failPanel:catgame.FailPanel;//输面板

        private isWin:number=0;//0 游戏中 1赢了 2输了

        public constructor(){
            super();
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
        }

        private onAddToStage(event:egret.Event){
            this.removeEventListener(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
            this.createGameScene();
        }

        private createGameScene():void{
            this.stageW=this.stage.stageWidth;
            this.stageH=this.stage.stageHeight;

            //地图
            this.map=new catgame.Map();
            this.map.init();
            this.map.x=35;
            this.map.y=this.stageH-460;
            this.addChild(this.map);

            //猫
            this.cat=new catgame.Cat();

            this.map.addEventListener("nodeClick",this.onNodeClick,this);
            this.map.addEventListener("weizhu",this.onWeiZhu,this);
            this.cat.addEventListener("catRun",this.onCatRun,this);

            //开始按钮
            this.btnStart=this.createBitmapByName("btnStart");
            this.btnStart.x=(this.stageW-this.btnStart.width)/2;
            this.btnStart.y=(this.stageH-this.btnStart.height)/2;
            this.btnStart.touchEnabled=true;
            this.btnStart.addEventListener(egret.TouchEvent.TOUCH_TAP,this.gameStart,this);
            this.addChild(this.btnStart);

            this.moreBtn=this.createBitmapByName("more_btn");
            this.moreBtn.x=(this.stageW-this.moreBtn.width)/2;
            this.moreBtn.y=this.stageH-this.moreBtn.height;
            this.moreBtn.touchEnabled=true;
            this.moreBtn.addEventListener(egret.TouchEvent.TOUCH_TAP,this.doMore,this);
            this.addChild(this.moreBtn);

            this.scorePanel=new catgame.ScorePanel();
            this.successPanel=new catgame.SuccessPanel();
            this.successPanel.addEventListener("shareEvent",this.doShare,this);
            this.successPanel.addEventListener("replayEvent",this.doRestart,this);

            this.failPanel=new catgame.FailPanel();
            this.failPanel.addEventListener("shareEvent",this.doShare,this);
            this.failPanel.addEventListener("replayEvent",this.doRestart,this);
        }

        /**
         * 玩家点击地图
         * @param e
         */
        private onNodeClick(e:egret.Event){
            console.log("onNodeClick");
            if(this.map.isExit(this.cat.node)){
                this.showResult(false);
                return;
            }
            this.map.lock()
            var pathes:any[]=this.map.findPath(this.cat.node);
            if(pathes.length){
                var path:number[]=pathes[0];
                var pos:number[]=this.map.coverPos2Point(path);
                this.cat.node=this.map.getNode(path);
                this.cat.run(pos);
            }else{
                this.showResult(true);
            }
        }

        private onWeiZhu(e:egret.Event){
            console.log("onWeiZhu");
            this.cat.weizhu();
        }

        private onCatRun(e:egret.Event){
            console.log("onCatRun");
            this.map.unlock();
        }

        private gameStart(e:egret.Event){
            console.log("gameStart");
            this.map.init();
            this.cat.init();
            this.cat.node=this.map.getNode([4,4]);

            var pos:number[]=this.map.coverPos2Point([4,4]);
            this.cat.x=pos[0];
            this.cat.y=pos[1]+10;
            this.addChild(this.cat);

            if(this.btnStart.parent){
                this.removeChild(this.btnStart);
            }

            if(this.successPanel.parent){
                this.removeChild(this.successPanel);
            }

            if(this.failPanel.parent){
                this.removeChild(this.failPanel);
            }

            if(this.moreBtn.parent){
                this.removeChild(this.moreBtn);
            }

            this.map.unlock();
            this.isWin=0;
        }

        private doMore(e:egret.Event){
            console.log("doMore");
        }

        private doShare(e:egret.Event){
            console.log("doShare");
        }

        private doRestart(e:egret.Event){
            console.log("doRestart");
            this.gameStart(null);
        }

        private showResult(b:Boolean){
            if(b){
                //赢了
                this.successPanel.x=(this.stageW-this.successPanel.width)/2;
                this.successPanel.y=(this.stageH-this.successPanel.height-this.moreBtn.height)/2;
                this.successPanel.score(this.map.tap);
                this.addChild(this.successPanel);
                this.isWin=1;

            }else{
                //输了
                this.failPanel.x=(this.stageW-this.failPanel.width)/2;
                this.failPanel.y=(this.stageH-this.failPanel.height)/2;
                this.addChild(this.failPanel);
                this.isWin=2;
            }
            this.addChild(this.moreBtn);
        }

        private createBitmapByName(name:string):egret.Bitmap{
            var result:egret.Bitmap=new egret.Bitmap();
            var texture:egret.Texture=RES.getRes(name);
            result.texture=texture;

            return result;
        }


    }
}
